package org.linlinjava.litemall.wx.web;


import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallHistories;
import org.linlinjava.litemall.db.service.LitemallHistoriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/wx/histories")
@Validated
public class LitemallHistoriesController {
    @Autowired
    private LitemallHistoriesService historiesService;


    @PostMapping("/addHistoryRecord")
    public Object addHistoryRecord(@RequestBody LitemallHistories histories) {
        if (histories.getUserId() <= 0 || histories.getGoodId() <= 0) {
            return ResponseUtil.fail(503, "参数出现异常");
        }
        histories.setCreatedAt(new Date());
        boolean isExists = false;
        List<LitemallHistories> latestHistory = historiesService.getHistoryRecords(histories.getUserId());
        for (LitemallHistories h : latestHistory) {
            if (Objects.equals(h.getGoodId(), histories.getGoodId())) {
                isExists = true;
                break;
            }
        }
        if (!isExists) {
           historiesService.insert(histories);
        }else {
            historiesService.updateHistory(histories);
        }
        return ResponseUtil.ok();
    }


    @GetMapping("/getHistoryRecords")
    public Object getHistoryRecords(@RequestParam(defaultValue = "10") int userId) {
        if (userId > 0) {
            List<LitemallHistories> historyRecords = historiesService.getHistoryRecords(userId);
            return ResponseUtil.ok(historyRecords);
        } else {
            return ResponseUtil.fail(503, "参数出现异常");
        }
    }

    /**
     * 获取用户历史记录数量
     * 
     * @param userId 用户ID
     * @return 历史记录数量
     */
    @GetMapping("/getHistoryCount")
    public Object getHistoryCount(@RequestParam int userId) {
        if (userId <= 0) {
            return ResponseUtil.fail(503, "参数出现异常");
        }
        
        try {
            int count = historiesService.getHistoryCount(userId);
            return ResponseUtil.ok(count);
        } catch (Exception e) {
            return ResponseUtil.fail(500, "获取历史记录数量失败");
        }
    }

    /**
     * 清空用户历史浏览记录
     * 
     * @param userId 用户ID
     * @return 操作结果
     */
    @PostMapping("/clearHistory")
    public Object clearHistory(@RequestParam int userId) {
        if (userId <= 0) {
            return ResponseUtil.fail(503, "参数出现异常");
        }
        
        try {
            int deletedCount = historiesService.clearUserHistory(userId);
            if (deletedCount >= 0) {
                return ResponseUtil.ok("成功清空 " + deletedCount + " 条历史记录");
            } else {
                return ResponseUtil.fail(500, "清空历史记录失败");
            }
        } catch (Exception e) {
            return ResponseUtil.fail(500, "清空历史记录时发生异常");
        }
    }
}